/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __SLE_LITE_LOG_H__
#define __SLE_LITE_LOG_H__

#ifndef SLE_HILOG_TAG
#define SLE_HILOG_TAG "SLE_LITE_SERVICE"
#endif

#include <stdio.h>

#define SLE_LOGI(format, ...) printf("[SLE_LITE_INFO] " format "\r\n", ##__VA_ARGS__)
#ifdef SLE_DEFAULT_LOG_LEVEL_DEBUG
#define SLE_LOGD(format, ...) printf("[SLE_LITE_DEBUG] " format "\r\n", ##__VA_ARGS__)
#else
#define SLE_LOGD(format, ...)
#endif
#define SLE_LOGW(format, ...) printf("[SLE_LITE_WARNING] " format "\r\n", ##__VA_ARGS__)
#define SLE_LOGE(format, ...) printf("[SLE_LITE_ERROR] " format "\r\n", ##__VA_ARGS__)

/**
 * @brief assert condition and return if failed.
 *
 * @param cond condition to assert
 * @return None
 */
#define SLE_ASSERT_AND_RETURN(cond)                              \
    do {                                                        \
        if (!(cond)) {                                          \
            SLE_LOGE("%s, assert failed!", #cond);               \
            return;                                             \
        }                                                       \
    } while (0)

/**
 * @brief assert condition and return ret if failed.
 *
 * @param cond condition to assert
 * @param ret return value
 * @return ret
 */
#define SLE_ASSERT_AND_RETURN_RET(cond, ret)                                             \
    do {                                                                                \
        if (!(cond)) {                                                                  \
            if (ret == 0) {                                                             \
                SLE_LOGW("%s, assert failed! ret: %s.", #cond, #ret);                    \
            } else {                                                                    \
                SLE_LOGE("%s, assert failed! ret: %s.", #cond, #ret);                    \
            }                                                                           \
            return ret;                                                                 \
        }                                                                               \
    } while (0)

/**
 * @brief assert condition and print log if failed.
 *
 * @param cond condition to assert
 * @param fmt log format
 * @return do not return
 */
#define SLE_ASSERT_AND_RETURN_LOG(cond, fmt, ...)    \
    do {                                            \
        if (!(cond)) {                              \
            SLE_LOGE(fmt, ##__VA_ARGS__);           \
            return;                                 \
        }                                           \
    } while (0)

/**
 * @brief assert condition and print log if failed.
 *
 * @param cond condition to assert
 * @param fmt log format
 * @return do not return
 */
#define SLE_ASSERT_AND_PRINT_LOG(cond, fmt, ...)  \
    do {                                         \
        if (!(cond)) {                           \
            SLE_LOGE(fmt, ##__VA_ARGS__);        \
        }                                        \
    } while (0)

/**
 * @brief assert condition and return ret with log if failed.
 *
 * @param cond condition to assert
 * @param ret return value
 * @param fmt log format
 * @return ret
 */
#define SLE_ASSERT_AND_RETURN_RET_LOG(cond, ret, fmt, ...)   \
    do {                                                    \
        if (!(cond)) {                                      \
            if ( ret == 0) {                                \
                SLE_LOGW(fmt, ##__VA_ARGS__);               \
            } else {                                        \
                SLE_LOGE(fmt, ##__VA_ARGS__);               \
            }                                               \
            return ret;                                     \
        }                                                   \
    } while (0)

#endif //__SLE_LITE_LOG_H__